package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService cs;

    //新增分类
    @PostMapping
    public R add(@RequestBody Category category) {

        //调用service
        int rows = cs.add(category);

        if (rows > 0) {
            return R.success("新增分类成功！");
        } else {
            return R.error("新增分类失败！");
        }
    }

    //分页查询
    @GetMapping("/page")
    public R findPage(PageParam pageParam) {

        //调用service
        IPage<Category> page = cs.findPage(pageParam);

        return R.success(page);
    }

    //删除分类
    @DeleteMapping
    public R delete(long id) {

        //调用service
        int rows = cs.delete(id);

        if (rows > 0) {
            return R.success("删除分类成功！");
        } else {
            return  R.error("删除分类失败！");
        }
    }

    //修改分类
    @PutMapping
    public R update(@RequestBody Category category) {

        //调用service
        int rows = cs.update(category);

        if (rows > 0) {
            return R.success("修改分类成功！");
        } else {
            return R.error("修改分类失败！");
        }
    }

    /**
     * 根据类型查询分类数据
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R list(Integer type ) {

        //调用service
        List<Category> list = cs.findCategoryByType(type);

        return R.success(list);
    }
}
